package com.hymjweb.demo.hymj.system_maintenance.role.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.system_maintenance.role.bean.RolePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface RoleMapper {

    class RoleMapperProvider{

        public String queryByCnd(Map param) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_role t");
                WHERE("1=1 ");
            }}.toString();

            if (param != null) {
                String keyRoleName = DataTypeUtil.toStringObject(param.get("keyRoleName")); // 角色名称
                if(!DataTypeUtil.isEmptyStr(keyRoleName)){
                    sql += " and t.name like " + DataTypeUtil.quoteAllLikeStr(keyRoleName);
                }
            }
            return sql;
        }

        public String checkNameUnique(String name) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_role t");
                WHERE("t.name = #{name}");
            }}.toString();
            return sql;
        }


        public String getRolePojoByIds(String userRoleIds){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_role t");
                WHERE("t.id in (" + userRoleIds + ")");
            }}.toString();
            return sql;
        }

        public String getRolePojoById(String roleId){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_role t");
                WHERE("t.id = #{roleId}");
            }}.toString();
            return sql;
        }

        public String insert(RolePojo rolePojo) {
            String sql = new SQL() {{
                INSERT_INTO("sys_role");
                VALUES("id", "#{id}");
                VALUES("name", "#{name}");
                VALUES("comment", "#{comment}");
            }}.toString();
            return sql;
        }


        public String update(RolePojo rolePojo) {
            String sql = new SQL() {{
                UPDATE("sys_role t");
                SET("t.name = #{name}");
                SET("t.comment = #{comment}");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }
    }

    @Results(id = "roleMapperId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "comment", property = "comment")
    })
    @SelectProvider(type = RoleMapperProvider.class, method = "queryByCnd")
    List<RolePojo> queryByCnd(Map param);

    @ResultMap("roleMapperId")
    @SelectProvider(type = RoleMapperProvider.class, method = "getRolePojoByIds")
    List<RolePojo> getRolePojoByIds(String userRoleIds);

    @ResultMap("roleMapperId")
    @SelectProvider(type = RoleMapperProvider.class, method = "checkNameUnique")
    RolePojo checkNameUnique(String name);

    @ResultMap("roleMapperId")
    @SelectProvider(type = RoleMapperProvider.class, method = "getRolePojoById")
    RolePojo getRolePojoById(String roleId);


    @InsertProvider(type = RoleMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(RolePojo rolePojo);

    @UpdateProvider(type = RoleMapperProvider.class, method = "update")
    int update(RolePojo rolePojo);


}
